package com.sunnay.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunnay.entity.DepositInfoEntity;
import com.sunnay.entity.ShopInfoEntity;
import com.sunnay.enums.GbCodeEnum;
import com.sunnay.exception.DataException;
import com.sunnay.mapper.ShopInfoMapper;
import com.sunnay.service.BidLogService;
import com.sunnay.service.DepositInfoService;
import com.sunnay.service.ShopInfoService;
import com.sunnay.util.WptUtils;
import com.sunnay.vo.auth.SystemUser;
import com.sunnay.vo.common.Page;
import com.sunnay.vo.product.ProductDetailVo;
import com.sunnay.vo.product.ProductVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service
public class ShopInfoServiceImpl extends ServiceImpl<ShopInfoMapper, ShopInfoEntity> implements ShopInfoService {

    @Resource
    private DepositInfoService depositInfoService;

    @Resource
    private BidLogService bidLogService;

    @Override
    public List<ShopInfoEntity> shopList(SystemUser systemUser) {
        return this.list(Wrappers.<ShopInfoEntity>query().lambda().eq(ShopInfoEntity::getWxId,systemUser.getWxId()).orderByDesc(ShopInfoEntity::getIsTop));
    }

    @Override
    @Transactional
    public void deleteShop(SystemUser systemUser, String id) {
        this.remove(Wrappers.<ShopInfoEntity>query().lambda().eq(ShopInfoEntity::getWxId,systemUser.getWxId()).eq(ShopInfoEntity::getId,id));
    }

    @Override
    public List<ProductVo> productList(String shopUri, SystemUser systemUser, Page page) {
        ShopInfoEntity one = this.getOne(Wrappers.<ShopInfoEntity>query().lambda().eq(ShopInfoEntity::getShopUri, shopUri).eq(ShopInfoEntity::getWxId, systemUser.getWxId()));
        Optional.ofNullable(one).orElseThrow(()-> new DataException(GbCodeEnum.SYSTEM_DATA_NOT_FOUND_ERROR.getKey(),GbCodeEnum.SYSTEM_DATA_NOT_FOUND_ERROR.getValue())) ;
        List<ProductVo> list = WptUtils.productList(shopUri, page);
        Optional.ofNullable(list).ifPresent(item->{
            item.forEach(data->{
                DepositInfoEntity depositInfo = depositInfoService.getDepositInfo(data.getUri());
                if(ObjectUtil.isNotEmpty(depositInfo)){
                    data.setDepositPrice(depositInfo.getAiniMoney());
                    data.setDepositStatus(depositInfo.getStatus());
                    data.setMark(bidLogService.selectCurrentUserInfo(data.getUri(),data.getNowPrice()));
                }else{
                    data.setDepositStatus(GbCodeEnum.USER_DEPOSIT_NOT_FOUND.getValue());
                }
            });
        });
        return list;
    }

    @Override
    public ProductDetailVo productFlush(String saleUri, SystemUser systemUser) {
        ProductDetailVo productDetailVo = WptUtils.productDetailInfo(saleUri);
        Optional.ofNullable(productDetailVo).orElseThrow(()->new DataException("未获取到拍品详细信息，请重试"));
        log.debug("最新价格："+productDetailVo.getNowPrice());
        //查看当前小号是否是本平台的
        productDetailVo.setMark(bidLogService.selectCurrentUserInfo(productDetailVo.getUri(),productDetailVo.getNowPrice()));
        return productDetailVo;
    }

    @Override
    @Transactional
    public void cancelBind(SystemUser systemUser, String id) {
        ShopInfoEntity one = this.getById(id);
        Optional.ofNullable(one).orElseThrow(()-> new DataException(GbCodeEnum.SYSTEM_DATA_NOT_FOUND_ERROR.getKey(),GbCodeEnum.SYSTEM_DATA_NOT_FOUND_ERROR.getValue())) ;
        this.baseMapper.deleteByShop(id);
    }

    @Override
    @Transactional
    public void topShop(String id, SystemUser systemUser) {
        ShopInfoEntity one = this.getById(id);
        Optional.ofNullable(one).orElseThrow(()-> new DataException(GbCodeEnum.SYSTEM_DATA_NOT_FOUND_ERROR.getKey(),GbCodeEnum.SYSTEM_DATA_NOT_FOUND_ERROR.getValue())) ;
        this.baseMapper.topShop(id);
    }

    @Override
    public void cancelTop(String id, SystemUser systemUser) {
        ShopInfoEntity one = this.getById(id);
        Optional.ofNullable(one).orElseThrow(()-> new DataException(GbCodeEnum.SYSTEM_DATA_NOT_FOUND_ERROR.getKey(),GbCodeEnum.SYSTEM_DATA_NOT_FOUND_ERROR.getValue())) ;
        this.baseMapper.cancelTopShop(id);
    }
}
